<?php

namespace App\Admin\Controllers;

use App\Building;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\AreaBuild;
use App\Park;
use App\BuildMatch;
use App\Broker;
use App\City;
use Carbon\Carbon;

class BuildingController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {

        return $content
            ->header('写字楼管理')
            ->description('写字楼管理')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('写字楼管理')
            ->description('写字楼管理')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('写字楼编辑')
            ->description('写字楼编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建写字楼')
            ->description('新建写字楼')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Building);

        $grid->id('Id');
        $grid->title('标题');
        //$grid->slider('轮播图');
        $grid->area('面积');
        //$grid->area_build_id('所属面积范围');
        $grid->rent('租金');
        $grid->address('地址');
        $grid->park_id('所属园区');
        $grid->is_company('是否注册公司')->display(function ($value) {
            if ($value == 0) {
                return '否';
            } else if ($value == 1) {
                return '是';
            }
        });
        $grid->use_rate('使用率');
        $grid->is_separate('是否分割')->display(function ($value) {
            if ($value == 0) {
                return '否';
            } else if ($value == 1) {
                return '是';
            }
        });
        $grid->renovation('装修情况');
        $grid->floor('所属楼层');
        //$grid->build_match_id('配套设施');
        $grid->contact('经纪人')->display(function($value){
            return Broker::find($value)->name ?? '';
        });
        //$grid->telephone('联系电话');
        $grid->is_owner('是否业主')->display(function ($value) {
            if ($value == 0) {
                return '否';
            } else if ($value == 1) {
                return '是';
            }
        });
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        //筛选功能
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $type = [
                    ' ' => '全部',
                    0 => '否',
                    1 => '是',
                ];
                $filter->disableIdFilter();
                $filter->equal('is_company', '是否注册公司')->select($type);
                $filter->equal('is_separate', '是否分割')->select($type);
                $filter->equal('is_owner', '是否业主')->select($type);
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', '写字楼名称');
                $filter->equal('area_build_id', '园区')->select(AreaBuild::getSelectOptions());
            });
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Building::findOrFail($id));

        $show->id('Id');
        $show->title('标题');
        $show->slider('轮播图');
        $show->area('面积');
        $show->area_build_id('所属面积范围');
        $show->rent('租金');
        $show->address('地址');
        $show->park_id('所属园区');
        $show->is_company('是否注册公司');
        $show->use_rate('使用率');
        $show->is_separate('是否分割');
        $show->renovation('装修情况');
        $show->floor('所属楼层');
        //$show->build_match_id('配套设施');
        $show->contact('联系人')->as(function($value){
            return Broker::find($value)->name;
        });
        //$show->telephone('联系电话');
        $show->is_owner('是否业主');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Building);
        $form->select('p_province_id', '城市')->options(City::getSelectOptions())->load('p_city_id', '/admin/api/area')->rules('required', [
            'required' => '请选择城市'
        ]);
        $form->select('p_city_id', '区域')->options(function ($id) {
            $id = $id ?: old('p_city_id');
            if($id)
            return City::option($id);
        })->load('p_district_id', '/admin/api/area1')->rules('required', [
            'required' => '请选择区域'
        ]);
        $form->select('p_district_id', '下级区域')->options(function ($id) {
            $id = $id ?: old('p_district_id');
            if($id)
            return City::option($id);
        });
        $form->text('title', '标题')->rules('required', [
            'required' => '请输入标题'
        ])->help('必填项');
        $form->uploader('slider', '轮播图')->rules('required', [
            'required' => '请插入轮播图'
        ])->help('必填项');
        $form->tags('tag', '标签')->help('添加输入后点击空白处');
        $form->hidden('code', '写字楼编号')->default(Carbon::now()->timestamp);

        $form->number('area', '面积(平方米)')->rules('required', [
            'required' => '请输入写字楼面积'
        ]);
        /*$form->select('area_build_id', '面积范围')->options(AreaBuild::getSelectOptions())->rules('required', [
            'required' => '请选择面积范围'
        ]);*/
        $form->currency('rent', '租金')->symbol("元/㎡/月")->help('不填写则面议');
        $form->currency('manage_cost', '管理费用')->symbol("元/㎡/月")->help('必填');
        $form->text('address', '地址')->rules('required', [
            'required' => '请输入地址'
        ])->help('必填项');
        //$form->map('latitude', 'longitude', '地址与周边设施');
        $form->select('park_id', '所属园区')->options(Park::getSelectOptions());
        $type = [
            0 => '否',
            1 => '是',
        ];
        $form->radio('is_company', '是否注册公司')->options($type);
        $states = [
            'on' => ['value' => 1, 'text' => '推荐', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '不推荐', 'color' => 'danger'],
        ];

        $form->switch('is_recommend', '是否推荐')->states($states);
        $form->number('use_rate', '使用率(%)')->rules('required', [
            'required' => '请输入使用率'
        ]);
        $form->radio('is_separate', '是否分割')->options($type);
        $renovation_type = [
            0 => '毛坯房',
            1 => '精装修',
        ];
        $form->radio('renovation', '装修情况')->options($renovation_type);
        $form->number('floor', '所属楼层')->rules('required', [
            'required' => '请输入所在楼层'
        ]);
        $form->text('standard_height', '标准层高')->rules('required', [
            'required' => '请输入内容'
        ])->help('必填项');
        $form->multipleSelect('build_match_id', '配套设施')->options(BuildMatch::getSelectOptions())->rules('required', [
            'required' => '请选择含有配套'
        ]);
        $form->select('contact', '经纪人')->options(Broker::getSelectOptions())->rules('required', [
            'required' => '请选择经纪人'
        ])->help('必填项');
        //$form->mobile('telephone', '联系电话')->options(['mask' => '999 9999 9999']);
        $form->radio('is_owner', '是否业主')->options($type);
        $form->lceditor('desc','描述');
        return $form;
    }
}
